package com.example.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.eduservice.entity.EduChapter;
import com.example.eduservice.entity.EduVideo;
import com.example.eduservice.entity.chapter.ChapterVo;
import com.example.eduservice.entity.chapter.VideoVo;
import com.example.eduservice.mapper.EduChapterMapper;
import com.example.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.eduservice.service.EduVideoService;
import com.example.servicebase.exceptionhandler.GuiliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService videoService;//注入小节的service,不注入的话，chapter里无法查小节

    //课程大纲列表,根据课程id进行查询,courseId课程id参数值
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //1 根据课程id查询里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChaptersList = baseMapper.selectList(wrapperChapter);


        //2 根据课程id查询里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo=new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);

        //创建List集合，用于最终封装数据
        List<ChapterVo> finalList=new ArrayList<>();

        //3 遍历查询章节list集合进行封装
        //遍历查询章节list集合
        for (int i = 0; i < eduChaptersList.size(); i++) {//eduChaptersList.fori回车
            //得到每个章节
            EduChapter eduChapter = eduChaptersList.get(i);
            //eduChapter中的值复制到ChapterVo中，最终放到list集合
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);//eduChapter中的值复制到ChapterVo
            //把chapterVo放到最终list集合
            finalList.add(chapterVo);

            //创建集合，用于封装章节中的小节
            List<VideoVo> VideoList=new ArrayList<>();
            //4 遍历查询小节list集合进行封装,一个章节中有多个小节，所以遍历小节时，要写for循环里面
            for (int m = 0; m < eduVideoList.size(); m++) {
                //得到每个小节
                EduVideo eduVideo = eduVideoList.get(m);
                //判断：小节里的chapter_id和章节中id一样就做封装
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    //进行封装,eduVideo中的值放到videoVo中
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    //放到小节封装集合
                    VideoList.add(videoVo);

                }

            }
            //把封装之后小节list集合，放到章节对象(chapterVo)中
            chapterVo.setChildren(VideoList);
        }
        return finalList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterid章节id 查询小节表，如果查询到数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = videoService.count(wrapper);//count表示对条件能查询到几条记录
        //判断
        if (count > 0) {//能查询出小节，不进行删除
            throw new GuiliException(20001, "不能删除");//new一个异常
        } else {//不能查询出小节，进行删除
            //删除章节
            int result = baseMapper.deleteById(chapterId);//传入章节id
            //成功 1>0 true ,失败则相反
            return result>0;
        }
    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
